package edu.thu.thss.yxy.gui;

import javax.swing.table.*;
import java.util.Properties;

public class PropertiesTableModel
    extends AbstractTableModel
{
    public PropertiesTableModel()
    {
    }

    final String[] columnNames = {
        "Property Name",
        "Property Value"};
    private Properties data;
    void setProperties(Properties properties)
    {
        this.data = properties;
    }

    Properties getProperties()
    {
        return data;
    }

    public int getColumnCount()
    {
        return columnNames.length;
    }

    public int getRowCount()
    {
        if (data == null) {
            return 0;
        }
        return data.size();
    }

    public String getColumnName(int col)
    {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col)
    {
        switch (col) {
            case 0:
                return data.keySet().toArray()[row];
            case 1:
                return data.get(data.keySet().toArray()[row]);
            default:
                return null;
        }
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class<?> getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col)
    {
        if (col == 1) {
            return true;
        }
        else {
            return false;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col)
    {
        String propName = getValueAt(row, 0).toString();
        data.setProperty(propName, value.toString());
    }
}